package com.food.admin.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 写入数据并设置有效时间
     */
    public void setEx(String key, String value,Integer seconds) throws Exception {
        stringRedisTemplate.opsForValue().set(key, value,seconds, TimeUnit.SECONDS);
    }
    public void setEx(String key, String value) throws Exception {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    /**
     * 读取数据
     */
    public String get(String key) throws Exception{
        return stringRedisTemplate.opsForValue().get(key);
    }


    /**
     * 删除数据
     */
    public void del(String key) throws Exception{
        stringRedisTemplate.delete(key);
    }
    /**
     * 判断存不存在
     */
    public boolean exist(String key) throws Exception{
        return stringRedisTemplate.hasKey(key);
    }
    /**
     * 自增方法
     */
    public void incr(String key)throws Exception {
        stringRedisTemplate.opsForValue().increment(key);
    }
    /**
     * 添加set数据
     */

    public void sadd(String key,String value) throws Exception{
        stringRedisTemplate.opsForSet().add(key, value);
    }
    /**
     * 判断set中是否存在某个值
     */
    public boolean sisMember(String key,String value) throws Exception{
        return stringRedisTemplate.opsForSet().isMember(key, value);
    }
    /**
     * 删除set中的某一个值
     */
    public void sremove(String key,String value) throws Exception {
        stringRedisTemplate.opsForSet().remove(key, value);
    }
    /**
     * 查询set数据
     */
    public Set<String> smembers(String key) throws Exception{
        return stringRedisTemplate.opsForSet().members(key);
    }
    /**
     * 向zset中添加数据
     */
    public void zadd(String key,String value,double score) throws Exception{
        stringRedisTemplate.opsForZSet().add(key, value, score);
    }
    /**
     * zset排序值自增方法
     */
    public void zincrby(String key,String value,double number) throws Exception{
        stringRedisTemplate.opsForZSet().incrementScore(key, value, number);
    }
    /**
     * 删除zset中某一个值的方法
     */
    public void zremove(String key,String value) throws Exception{
        stringRedisTemplate.opsForZSet().remove(key, value);
    }
    /**
     * 查询zset排序值
     */
    public double zscore(String key,String value) throws Exception{
        return stringRedisTemplate.opsForZSet().score(key, value);
    }
    /**
     * 按照排序值从大到小查询某一段数据
     */
    public Set<String> zReverseRange(String key,Integer start,Integer end) throws Exception{
        return stringRedisTemplate.opsForZSet().reverseRange(key, start, end);
    }
    /**
     * hash属性自增的方法
     */
    public void hincrby(String key,String hashKey,Integer number) throws Exception{
        stringRedisTemplate.opsForHash().increment(key,hashKey,number);
    }
    /**
     * 新增一个hash数据
     */
    public void hmset(String key, HashMap<String,Object> map) throws Exception{
        stringRedisTemplate.opsForHash().putAll(key,map);
    }

    /**
     * 获取hash中某一个属性值
     */
    public Object hget(String key,String hashKey) throws Exception{
        return stringRedisTemplate.opsForHash().get(key,hashKey);
    }

    /**
     * 返回 cart：userId： list
     * */
    public Set<String> sget(String key){
        Set<String> set = stringRedisTemplate.opsForSet().members(key);
        return set;
    }
    /**
     * 判断hash中是否存在该键
     * */
    public Map<Object,Object> hismenber(String key){
        return stringRedisTemplate.opsForHash().entries(key);
    }
}
